<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Contacts_c extends CI_Controller {
        
        function __construct() {
            parent::__construct();
            $this->load->model(array('client_m', 'coordinator_m', 'supplier_m'));
            $config = array(
                'protocol' => 'smtp',
                'smtp_host' => 'ssl://smtp.gmail.com',
                'smtp_user' => 'dummy.itproject@gmail.com',
                'smtp_pass' => 'itdummyproject12345',
                'smtp_port' => 465,
            );
            $this->load->library('email', $config);
        }
        
        function index() {
            $data['content'] = 'pages/contacts/contacts';
            $data['title'] = 'Contacts';
            $data['current_page'] = 'contacts-page';
            $data['back_to'] = 'contacts';
            $data['employees'] = $this->coordinator_m->get_employees($this->session->userdata('accid'));
            $data['clients'] = $this->client_m->get_clients();
            $data['supplier'] = $this->supplier_m->get_all_supplier();
            $this->load->view('includes/template', $data);
        }
        
        function employees_ajax() {
            echo json_encode(array('aaData' => $this->coordinator_m->get_employees($this->session->userdata('accid'))));
        }
        
        function save_employee() {
            $pass = $this->generate_password();
            $emp_info = array(
                'coord_lname' => $this->input->post('lname'),
                'coord_fname' => $this->input->post('fname'),
                'coord_mname' => $this->input->post('mname'),
                'coord_bday' => $this->input->post('bday'),
                'coord_gender' => $this->input->post('gender'),
                'coord_address' => $this->input->post('address'),
                'coord_num' => $this->input->post('contact_no'),
                'coord_email' => $this->input->post('email'),
                'coord_pw' => sha1($pass),
                'emp_date' => $this->input->post('date_emp'),
                'coord_status' => '0',
            );
            $status = '';
            $msg = '';
            $emp_data = '';
            $emp_id = $this->input->post('emp_id');
            if($this->input->post('action')==='0'){
                if($this->coordinator_m->check_if_emp_exist($emp_info['coord_lname'], $emp_info['coord_fname'], $emp_info['coord_mname'])<1){
                    $id = $this->coordinator_m->insert_emp($emp_info);
                    if($id){
                        $emp_result = $this->coordinator_m->get_employee($id);
                        $emp_email = explode(';', $emp_info['coord_email']);
                        for($i=0;$i<sizeof($emp_email);$i++){
                            $this->email->set_newline("\r\n");
                            $this->email->from($this->session->userdata('email'), $this->session->userdata('lname').', '.$this->session->userdata('fname'));
                            $this->email->to($emp_email[$i]);
                            $this->email->subject("Your password from BBEM");
                            $this->email->message($pass);
                            $this->email->send();   
                        }
                        mkdir('uploads/'.$id);
                        $status = 'success';
                        $msg = 'Employee <b>'.$emp_result->coord_lname.', '.$emp_result->coord_fname.' '.$emp_result->coord_mname.'</b> successfully saved.';
                        $emp_data = $emp_result;
                    }else{
                        $status = 'error';
                        $msg = 'Something went wrong while saving the employee. Please try again later.';
                        $emp_data = '';
                    }
                }else{
                    $status = 'error';
                    $msg = 'Employee already exist.';
                    $emp_data = 'already exist';
                }
            }elseif($this->input->post('action')==='1'){
                $id = $this->coordinator_m->update_emp($emp_id, $emp_info);
                if($id > 0){
                    $emp = $this->coordinator_m->get_employee($emp_id);
                    $status = 'success';
                    $msg = $msg = 'Employee <b>'.$emp->coord_lname.', '.$emp->coord_fname.' '.$emp->coord_mname.'</b> successfully updated.';
                    $emp_data = $emp;
                }else{
                    $status = 'error';
                    $msg = 'Something went wrong while updating the employee. Please try again later.';
                    $emp_data = '';
                }
            }
            echo json_encode(array('status' => $status, 'msg' => $msg, 'emp'=>$emp_data));
        }
        
        function view_employee($id) {
            $emp = $this->coordinator_m->get_employee($id);
            echo json_encode($emp);
        }
        
        function delete_employee() {
            $emp_id = $this->input->post('emp_id');
            $msg = '';
            $this->coordinator_m->delete_employee($emp_id);
            if(sizeof($emp_id)>1){
                $msg = 'Employees successfully deleted.';
            }else{
                $msg = 'Employee successfully deleted.';
            }
            echo json_encode(array('msg'=>$msg));
        }
        
        function clients_ajax(){
            echo json_encode(array('aaData'=>$this->client_m->get_clients()));
        }
        
        function save_client() {
            $pass = $this->generate_password();
            $client_info = array(
                'client_fname' => $this->input->post('fname'),
                'client_lname' => $this->input->post('lname'),
                'client_address' => $this->input->post('address'),
                'client_num' => $this->input->post('contact_no'),
                'client_email' => $this->input->post('email'),
                'client_pw' => sha1($this->generate_password()),
            );
            $status = '';
            $msg = '';
            $client_data = '';
            $client_id = $this->input->post('client_id');
            if($this->input->post('action')==='0'){    
                if($this->client_m->check_if_client_exist($client_info['client_lname'],$client_info['client_fname'])<1){
                    $id = $this->client_m->insert_client($client_info);
                    if($id){
                        $client = $this->client_m->get_client($id);
//                        $client_email[] = explode(';',$client_info['client_email']);
//                        for($i=0;$i<sizeof($client_email);$i++){
//                            $this->email->set_newline("\r\n");
//                            $this->email->from($this->session->userdata('email'), $this->session->userdata('lname').', '.$this->session->userdata('fname'));
//                            $this->email->to($client_email[$i]);
//                            $this->email->subject("Your password from BBEM");
//                            $this->email->message($pass);
//                            $this->email->send();
//                        }
                        $status = 'success';
                        $msg = 'Client <b>'.$client->client_lname.', '.$client->client_fname.'</b> successfully saved.';
                        $client_data = $client;
                    }else{
                        $status = 'error';
                        $msg = 'Something went wrong while saving the client. Please try again later.';
                        $client_data = '';
                    }   
                }else{
                    $status = 'error';
                    $msg = 'Client already exist.';
                    $client_data = 'already exist';
                }
            }elseif($this->input->post('action')==='1'){
                $id = $this->client_m->update_client($client_id, $client_info);
                if($id > 0){
                    $status = 'success';
                    $client = $this->client_m->get_client($client_id);
                    $msg = 'Client <b>'.$client->client_lname.', '.$client->client_fname.'</b> successfully updated.';
                    $client_data = $client;
                }else{
                    $status = 'error';
                    $msg = 'Something went wrong while updating the client. Please try again later.';
                    $client_data = '';
                }
            }
            echo json_encode(array('status' => $status, 'msg' => $msg, 'client'=>$client_data));
        }
        
        function view_client($id) {
            $client = $this->client_m->get_client($id);
            echo json_encode($client);
        }
        
        function delete_client() {
            $client_id = $this->input->post('client_id');
            $this->client_m->delete_client($client_id);
            if(sizeof($client_id)>1){
                $msg = 'Clients successfully deleted.';
            }else{
                $msg = 'Client successfully deleted.';
            }
            echo json_encode(array('msg'=>$msg));
        }
        
        function get_supplier(){
            $status = '';
            $data = '';
            $supplier = $this->supplier_m->get_supplier($this->input->post('supplier_id'));
            if($supplier!==FALSE){
                $status = 'success';
                $data = $supplier;
            }else{
                $status = 'error';
                $data = 'Cannot get the supplier.';
            }
            echo json_encode(array('status'=>$status, 'supplier'=>$data));
        }
        
        function supplier_ajax(){
            echo json_encode(array('aaData' => $this->supplier_m->get_all_supplier()));
        }
        
        function event_supplier(){
            echo json_encode($this->supplier_m->get_all_supplier());
        }
        
        function save_supplier() {
            $supplier_info = array(
                'company' => $this->input->post('cname'),
                'contact_person' => $this->input->post('contact_person'),
                's_num' => $this->input->post('contact_no'),
                's_email' => $this->input->post('email'),
                'services' => $this->input->post('services'),
                's_address' => $this->input->post('address'),
                's_status' => 'active',
            );
            $status = '';
            $msg = '';
            $supplier_data = '';
            $supplier_id = $this->input->post('supplier_id');
            if($this->input->post('action')==='0'){
                if($this->supplier_m->check_if_supplier_exist($supplier_info['company'])<1){
                    $id = $this->supplier_m->insert_supplier($supplier_info);
                    if($id){
                        $supplier = $this->supplier_m->get_supplier($id);
                        $status = 'success';
                        $msg = 'Supplier <b>'.$supplier->company.'</b> successfully saved.';
                        $supplier_data = $supplier;
                    }else{
                        $status = 'error';
                        $msg = 'Something went wrong while saving the supplier. Please try again later.';
                        $supplier_data = '';
                    }
                }else{
                    $status = 'error';
                    $msg = 'Supplier already exist.';
                    $supplier_data = 'already exist';
                }
            }elseif($this->input->post('action')==='1'){
                $id = $this->supplier_m->update_supplier($supplier_id, $supplier_info);
                if($id > 0){
                    $supplier = $this->supplier_m->get_supplier($supplier_id);
                    $status = 'success';
                    $msg = 'Supplier <b>'.$supplier->company.'</b> successfully updated.';
                    $supplier_data = $supplier;
                }else{
                    $status = 'error';
                    $msg = 'Something went wrong while updating the supplier. Please try again later.';
                    $supplier_data = '';
                }
            }
            echo json_encode(array('status' => $status, 'msg' => $msg, 'supplier'=>$supplier_data));
        }
        
        function save_service(){
            $status = '';
            $msg = '';
            $id = $this->input->post('supplier_id');
            $current_services = $this->supplier_m->get_services($id);
            $services = $current_services->services.';'.$this->input->post('service');
            $result = $this->supplier_m->update_service($id, $services);
            if($result>0){
                $status = 'success';
                $msg = 'Service successfully added.';
            }else{
                $status = 'error';
                $msg = 'Something went wrong while saving the services. Please try again later.';
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
        
        function view_supplier($id) {
            echo json_encode($this->supplier_m->get_supplier($id));
        }
        
        function delete_supplier() {
            $this->supplier_m->delete_supplier($this->input->post('supplier_id'));
        }
        
        function generate_password() {
            // generate random characters
            $rand_no = substr(str_shuffle('0123456789'), 0, 3);
            $rand_sletter = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, 3);
            $rand_bletter = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 3);
            $pass = str_shuffle($rand_no . $rand_sletter . $rand_bletter);
            return $pass;
        }
    }
